package com.konsung.presenter.impl;

import com.konsung.bean.MeasureDataBean;
import com.konsung.bean.PatientBean;
import com.konsung.greendaoUtil.GreenDaoManager;
import com.konsung.greendaoUtil.MeasureDataBeanDao;
import com.konsung.greendaoUtil.PatientBeanDao;
import com.konsung.healthfile.R;
import com.konsung.kslibrary.base.BasePresenter;
import com.konsung.presenter.ReportListPresenter;
import com.konsung.ui.ReportListActivity;
import com.konsung.util.BroadcastUtils;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.List;

/**
 * Created by xiangshicheng on 2019/1/4 0004.
 * 体检报告逻辑实现类
 */

public class ReportListPresenterImpl extends BasePresenter<ReportListActivity>
        implements ReportListPresenter.Presenter {

    @Override
    public String getAllStr(PatientBean patientBean) {
        String allStr = ""; //拼接字符串
        String[] sexArr = mvpView.getResources().getStringArray(R.array.sex_type);
        String[] patientTypeArr = mvpView.getResources().getStringArray(R.array.patient_type);
        int sex = patientBean.getSex();
        int patientType = patientBean.getPatientType();
        if (sex == 1) {
            //男
            sex = 0;
        } else if (sex == 2) {
            sex = 1;
        } else {
            sex = 2;
        }
        allStr = patientBean.getName() + " " + sexArr[sex] + " " + patientTypeArr[patientType];
        return allStr;
    }

    @Override
    public List<MeasureDataBean> getMeasureDataList(String idCard, int pageNum) {
        List<MeasureDataBean> measureDataBeanList;
        MeasureDataBeanDao measureDataBeanDao = GreenDaoManager.getMeasureDao(mvpView);
        QueryBuilder queryBuilder = measureDataBeanDao.queryBuilder();
        WhereCondition whereCondition = MeasureDataBeanDao.Properties.Idcard.eq(idCard);
        queryBuilder.where(whereCondition);
        queryBuilder.offset(pageNum);
        queryBuilder.limit(6);
        queryBuilder.orderDesc(MeasureDataBeanDao.Properties.MeasureTime);
        queryBuilder.distinct();
        measureDataBeanList = queryBuilder.list();
        return measureDataBeanList;
    }

    @Override
    public long getMeasures(String idCard) {
        return GreenDaoManager.getMeasureDao(mvpView).queryBuilder()
                .where(MeasureDataBeanDao.Properties.Idcard.eq(idCard)).distinct().count();
    }

    @Override
    public List<MeasureDataBean> getAllMeasure(String idCard) {
        return GreenDaoManager.getMeasureDao(mvpView).queryBuilder()
                .where(MeasureDataBeanDao.Properties.Idcard.eq(idCard)).list();
    }

    @Override
    public void deleteSelectMeasures(List<MeasureDataBean> measureDataBeans) {
        MeasureDataBeanDao measureDataBeanDao = GreenDaoManager.getMeasureDao(mvpView);
        measureDataBeanDao.deleteInTx(measureDataBeans);
        BroadcastUtils.sendReplacePatientForAll();
        mvpView.refreshView();
    }

    @Override
    public PatientBean getPatientBean(String idCard) {
        PatientBean patientBean = null;
        List<PatientBean> list = GreenDaoManager.getPatientDao(mvpView).queryBuilder()
                .where(PatientBeanDao.Properties.Idcard.eq(idCard)).list();
        if (list != null && list.size() > 0) {
            patientBean = list.get(0);
        }
        return patientBean;
    }
}
